using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class HeadOverlaySubMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.hos_Menu, new Point(0, -107));

	private static List<dynamic> overlayIndexList = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> opacityList = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> colorList = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> colorTypeList = new List<object>(new string[1] { string.Empty });

	private static UIMenuListItem overlayIndexListItem;

	private static UIMenuListItem opacityListItem;

	private static UIMenuListItem firstColorListItem;

	private static UIMenuListItem secondColorListItem;

	private static UIMenuListItem colorTypeListItem;

	private static int oid;

	private static bool canEditColor;

	public HeadOverlaySubMenu()
	{
		Main.pool.Add(menu);
		menu.OnListChange += OnListChange;
		menu.OnItemSelect += OnItemSelect;
		menu.OnMenuClose += OnMenuClose;
	}

	public static void InitHeadOverlaySubMenu(int overlayId)
	{
		oid = overlayId;
		menu.Clear();
		overlayIndexList = new List<object>();
		opacityList = new List<object>();
		colorList = new List<object>();
		colorTypeList = new List<object>();
		Main.P1.skin.GetHeadOverlayVariableNumber();
		for (int i = 0; i < Main.P1.skin.Head_Overlay[oid].overlayNumber; i++)
		{
			overlayIndexList.Add(i);
		}
		canEditColor = Array.IndexOf(Skin.HEAD_OVERLAY_CAN_EDIT_COLOR, oid) >= 0;
		for (double num = 0.0; num <= 1.0; num += 0.1)
		{
			opacityList.Add(Math.Round(num, 1));
		}
		for (int j = 0; j < Skin.HEAD_OVERLAY_COLOR_NUM; j++)
		{
			colorList.Add(j);
		}
		colorTypeList.Add(1);
		colorTypeList.Add(2);
		Skin.HeadOverlay headOverlay;
		if (DressingScript.Dressing == Main.Member.P2)
		{
			headOverlay = Main.P2.skin.Head_Overlay[oid];
		}
		else
		{
			headOverlay = Main.P1.skin.Head_Overlay[oid];
		}
		menu.SetBannerType(new UIResRectangle());
		overlayIndexListItem = new UIMenuListItem(TextSetting.hos_ListItem_Index, overlayIndexList, headOverlay.overlayValue);
		opacityListItem = new UIMenuListItem(TextSetting.hos_ListItem_Opacity, opacityList, opacityList.FindIndex(name => name == headOverlay.opacity));
		menu.AddItem(overlayIndexListItem);
		menu.AddItem(opacityListItem);
		if (canEditColor)
		{
			colorTypeListItem = new UIMenuListItem(TextSetting.hos_ListItem_ColorType, colorTypeList, colorTypeList.FindIndex(name => name == headOverlay.colorType));
			firstColorListItem = new UIMenuListItem(TextSetting.hos_ListItem_FirstColor, colorList, headOverlay.firstColor);
			secondColorListItem = new UIMenuListItem(TextSetting.hos_ListItem_SecondColor, colorList, headOverlay.secondColor);
			menu.AddItem(colorTypeListItem);
			menu.AddItem(firstColorListItem);
			menu.AddItem(secondColorListItem);
			menu.AddItem(new UIMenuItem(TextSetting.hos_MenuItem_RandomizeColor));
		}
		menu.AddItem(new UIMenuItem(TextSetting.hos_MenuItem_Transparent));
		menu.AddItem(new UIMenuItem(TextSetting.hos_MenuItem_Opaque));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
		menu.RefreshIndex();
	}

	private static void UpdateHeadOverlayValue(int overlayId)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.skin.Head_Overlay[oid].overlayValue = overlayIndexListItem.Index;
			Main.P2.skin.Head_Overlay[oid].opacity = (float)opacityList[opacityListItem.Index];
			if (canEditColor)
			{
				Main.P2.skin.Head_Overlay[oid].colorType = (int)colorTypeList[colorTypeListItem.Index];
				Main.P2.skin.Head_Overlay[oid].firstColor = firstColorListItem.Index;
				Main.P2.skin.Head_Overlay[oid].secondColor = secondColorListItem.Index;
			}
			return;
		}
		Main.P1.skin.Head_Overlay[oid].overlayValue = overlayIndexListItem.Index;
		Main.P1.skin.Head_Overlay[oid].opacity = (float)opacityList[opacityListItem.Index];
		Main.Clone.skin.Head_Overlay[oid].overlayValue = overlayIndexListItem.Index;
		Main.Clone.skin.Head_Overlay[oid].opacity = (float)opacityList[opacityListItem.Index];
		if (canEditColor)
		{
			Main.P1.skin.Head_Overlay[oid].colorType = (int)colorTypeList[colorTypeListItem.Index];
			Main.P1.skin.Head_Overlay[oid].firstColor = firstColorListItem.Index;
			Main.P1.skin.Head_Overlay[oid].secondColor = secondColorListItem.Index;
			Main.Clone.skin.Head_Overlay[oid].colorType = (int)colorTypeList[colorTypeListItem.Index];
			Main.Clone.skin.Head_Overlay[oid].firstColor = firstColorListItem.Index;
			Main.Clone.skin.Head_Overlay[oid].secondColor = secondColorListItem.Index;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnListChange(UIMenu sender, UIMenuListItem list, int index)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			SkinHelper.SetPedHeadOverlayValue(Main.P2.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
			Main.P2.skin.Head_Overlay[oid].overlayAction = true;
			if (canEditColor)
			{
				SkinHelper.SetPedHeadOverlayColor(Main.P2.Character, oid, (int)colorTypeList[colorTypeListItem.Index], firstColorListItem.Index, secondColorListItem.Index);
				Main.P2.skin.Head_Overlay[oid].colorAction = true;
			}
			return;
		}
		SkinHelper.SetPedHeadOverlayValue(Main.P1.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
		SkinHelper.SetPedHeadOverlayValue(Main.Clone.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
		Main.P1.skin.Head_Overlay[oid].overlayAction = true;
		Main.Clone.skin.Head_Overlay[oid].overlayAction = true;
		if (canEditColor)
		{
			SkinHelper.SetPedHeadOverlayColor(Main.P1.Character, oid, (int)colorTypeList[colorTypeListItem.Index], firstColorListItem.Index, secondColorListItem.Index);
			SkinHelper.SetPedHeadOverlayColor(Main.Clone.Character, oid, (int)colorTypeList[colorTypeListItem.Index], firstColorListItem.Index, secondColorListItem.Index);
			Main.P1.skin.Head_Overlay[oid].colorAction = true;
			Main.Clone.skin.Head_Overlay[oid].colorAction = true;
		}
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		if (canEditColor)
		{
			switch (index)
			{
			case 5:
			{
				Random random = new Random(Guid.NewGuid().GetHashCode());
				colorTypeListItem.Index = random.Next(0, colorTypeList.Count);
				firstColorListItem.Index = random.Next(0, colorList.Count);
				secondColorListItem.Index = random.Next(0, colorList.Count);
				if (DressingScript.Dressing == Main.Member.P2)
				{
					SkinHelper.SetPedHeadOverlayColor(Main.P2.Character, oid, (int)colorTypeList[colorTypeListItem.Index], firstColorListItem.Index, secondColorListItem.Index);
					Main.P2.skin.Head_Overlay[oid].colorAction = true;
					break;
				}
				SkinHelper.SetPedHeadOverlayColor(Main.P1.Character, oid, (int)colorTypeList[colorTypeListItem.Index], firstColorListItem.Index, secondColorListItem.Index);
				SkinHelper.SetPedHeadOverlayColor(Main.Clone.Character, oid, (int)colorTypeList[colorTypeListItem.Index], firstColorListItem.Index, secondColorListItem.Index);
				Main.P1.skin.Head_Overlay[oid].colorAction = true;
				Main.Clone.skin.Head_Overlay[oid].colorAction = true;
				break;
			}
			case 6:
				opacityListItem.Index = 0;
				if (DressingScript.Dressing == Main.Member.P2)
				{
					SkinHelper.SetPedHeadOverlayValue(Main.P2.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
					Main.P2.skin.Head_Overlay[oid].overlayAction = false;
					break;
				}
				SkinHelper.SetPedHeadOverlayValue(Main.P1.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
				SkinHelper.SetPedHeadOverlayValue(Main.Clone.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
				Main.P1.skin.Head_Overlay[oid].overlayAction = false;
				Main.Clone.skin.Head_Overlay[oid].overlayAction = false;
				break;
			case 7:
				opacityListItem.Index = 10;
				if (DressingScript.Dressing == Main.Member.P2)
				{
					SkinHelper.SetPedHeadOverlayValue(Main.P2.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
					Main.P2.skin.Head_Overlay[oid].overlayAction = true;
					break;
				}
				SkinHelper.SetPedHeadOverlayValue(Main.P1.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
				SkinHelper.SetPedHeadOverlayValue(Main.Clone.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
				Main.P1.skin.Head_Overlay[oid].overlayAction = true;
				Main.Clone.skin.Head_Overlay[oid].overlayAction = true;
				break;
			case 8:
				DressingScript.Distance();
				break;
			case 9:
				DressingScript.TurnLeft();
				break;
			case 10:
				DressingScript.TurnRight();
				break;
			case 11:
				DressingScript.AngleUp();
				break;
			case 12:
				DressingScript.AngleDown();
				break;
			}
			return;
		}
		switch (index)
		{
		case 2:
			opacityListItem.Index = 0;
			if (DressingScript.Dressing == Main.Member.P2)
			{
				SkinHelper.SetPedHeadOverlayValue(Main.P2.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
				Main.P2.skin.Head_Overlay[oid].overlayAction = false;
				break;
			}
			SkinHelper.SetPedHeadOverlayValue(Main.P1.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
			SkinHelper.SetPedHeadOverlayValue(Main.Clone.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
			Main.P1.skin.Head_Overlay[oid].overlayAction = false;
			Main.Clone.skin.Head_Overlay[oid].overlayAction = false;
			break;
		case 3:
			opacityListItem.Index = 10;
			if (DressingScript.Dressing == Main.Member.P2)
			{
				SkinHelper.SetPedHeadOverlayValue(Main.P2.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
				Main.P2.skin.Head_Overlay[oid].overlayAction = true;
				break;
			}
			SkinHelper.SetPedHeadOverlayValue(Main.P1.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
			SkinHelper.SetPedHeadOverlayValue(Main.Clone.Character, oid, overlayIndexListItem.Index, (float)opacityList[opacityListItem.Index]);
			Main.P1.skin.Head_Overlay[oid].overlayAction = true;
			Main.Clone.skin.Head_Overlay[oid].overlayAction = true;
			break;
		case 4:
			DressingScript.Distance();
			break;
		case 5:
			DressingScript.TurnLeft();
			break;
		case 6:
			DressingScript.TurnRight();
			break;
		case 7:
			DressingScript.AngleUp();
			break;
		case 8:
			DressingScript.AngleDown();
			break;
		}
	}

	private void OnMenuClose(UIMenu sender)
	{
		UpdateHeadOverlayValue(oid);
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
